//
//  LetterDetailCell.m
//  Growth
//
//  Created by Hobo.Chen on 3/29/12.
//  Copyright (c) 2012 MEETStudio. All rights reserved.
//

#import "LetterDetailCell.h"

@implementation LetterDetailCell

@synthesize userId;

@synthesize iconButton;

@synthesize conttentView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

-(NSString *)changeStringFommat:(NSString *)str{
    
    PublicMethodEngine *methodEngine = [PublicMethodEngine sharedPublicMethodEngine];   
    
    return  [methodEngine changeStringFommat:str];
    
    
    
}


- (void)dealloc{
    
    [conttentView release];
    [contentLabel release];
    [timeLabel release];
    [iconImageView release];
    [iconButton release];
     
    [angleImageView release];
    
    [super dealloc];
}


- (void)loadCellWith:(NSMutableDictionary *)dict
{
	
     self.userId=[[[NSUserDefaults standardUserDefaults] valueForKey:@"userId"] intValue]; 
    
	
    contentLabel.lineBreakMode = UILineBreakModeWordWrap;
    contentLabel.highlightedTextColor = [UIColor whiteColor];
    contentLabel.numberOfLines = 0;
    contentLabel.opaque = NO; // 选中Opaque表示视图后面的任何内容都不应该绘制
    contentLabel.backgroundColor = [UIColor clearColor];
	
    NSString *content = [dict objectForKey:@"Body"];
    
    content = [self changeStringFommat:content];
    
    
    contentLabel.text = content;
  
    UIFont *font = [UIFont systemFontOfSize:14];
    CGSize size = CGSizeMake(180 ,2000);
    CGSize labelsize = [content sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap]; 
    
    
    if (labelsize.width < 80) {
        labelsize.width = 80;
    }
	
    
    
    if (self.userId == [[dict objectForKey:@"SenderID"] intValue]) { //自己发出去的,头像在右边
        
     
        [self.conttentView setFrame:CGRectMake(320 - 60 - labelsize.width -10, 10, labelsize.width+10, labelsize.height+30)];
        [contentLabel setFrame:CGRectMake(5, 5, labelsize.width, labelsize.height)];
        
                      
        
    }
    else{//头像在左边
        
        [self.conttentView setFrame:CGRectMake(60, 10, labelsize.width+10, labelsize.height+30)];
        [contentLabel setFrame:CGRectMake(5, 5, labelsize.width, labelsize.height)];
        
        
        
    }
    
          
   
    
	        //好友头像
        
       NSString *userIconImage;
        if([[dict objectForKey:@"fromUserAttachmentId"] isKindOfClass:[NSNull class]]){
            userIconImage=@"";
        }
        else{
            userIconImage=[dict objectForKey:@"fromUserAttachmentId"];
        }
        
       
    if(userIconImage!=@""){
        
        
               
        if (self.userId != [[dict objectForKey:@"SenderID"] intValue]){
           
            [iconImageView setFrame:CGRectMake(10, 10, 40, 40)];
            [iconButton setFrame:iconImageView.frame];
            iconButton.tag = [[dict objectForKey:@"fromUserId"] intValue];
            
        }
        else{
            
             [iconImageView setFrame:CGRectMake(270, 10, 40, 40)];   
             [iconButton setFrame:iconImageView.frame];
            iconButton.tag = self.userId;
        }
	    
        
       // NSString *string=[NSString stringWithFormat:@"%@%@/%@/100x100",BASEURL,@"/commons/attachment/download",userIconImage];
        
        NSString *string=[NSString stringWithFormat:@"%@/%@/%@/100x100",UCENTERBASEURL,@"gravatar",userIconImage];

        
        NSURL *url = [NSURL URLWithString:string];	
        
             
         [iconImageView setImageWithURL:url refreshCache:NO placeholderImage:[UIImage imageNamed:@"geren_set_iphone.png"] imageType:kImageCache_Content_Icon]; 
        
        [iconImageView setClipsToBounds:YES];
        iconImageView.layer.cornerRadius = 3;
               
	}
  
      
  
    
    [angleImageView setImage:[UIImage imageNamed:@"bg_arrow_iphone.png"]];
    
    if (self.userId == [[dict objectForKey:@"SenderID"] intValue]) {
        [angleImageView setImage:[UIImage imageNamed:@"bg_arrowR_iphone.png"]];
        
       // angleImageView.transform = CGAffineTransformMakeRotation(1.58*2);
        
        [angleImageView setFrame:CGRectMake(conttentView.frame.origin.x + conttentView.frame.size.width-2, 15, angleImageView.frame.size.width, angleImageView.frame.size.height)];
    }
    

        
    //日志时间
    
    NSString *date =  [dict objectForKey:@"PostDate"];
    
    date =  [date substringWithRange:NSMakeRange(6, 13)];
    
  long long int creatTime = [date longLongValue];
    creatTime = creatTime/1000;
    
    NSDate *nd = [NSDate dateWithTimeIntervalSince1970:creatTime];
        
        
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        
        [dateFormat setDateFormat:@"MM-dd HH:mm"];
        
        NSString *dateString = [dateFormat stringFromDate:nd];
        
        timeLabel.text = dateString;

    
    [timeLabel setFrame:CGRectMake(8, labelsize.height+10, labelsize.width, 20)];
    [timeLabel setTextColor:Text_Color];
      
    conttentView.layer.cornerRadius = 3;
       
    conttentView.layer.shadowOpacity = 0.4;
	conttentView.layer.shadowRadius = 1.5;
	conttentView.layer.shadowOffset = CGSizeMake(0, 0);
    CGRect shadowFrame = conttentView.layer.bounds;
    CLog(@"shadow frame rect %f, %f, %f, %f", shadowFrame.origin.x, shadowFrame.origin.y, shadowFrame.size.width, shadowFrame.size.height);
    CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(shadowFrame.origin.x, shadowFrame.origin.y, shadowFrame.size.width, shadowFrame.size.height +2)].CGPath;
    conttentView.layer.shadowPath = shadowPath;

    CGRect cellFrame = [self frame];
	cellFrame.origin = CGPointMake(0, 0);
    cellFrame.size.height = conttentView.frame.size.height + 20;
	[self setFrame:cellFrame];
    
}	



#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
